<?php
function dataPost($post_string, $url)
{ //POST方式提交数据
    $context = array('http' => array(
        'method' => "POST",
        'header' => "Connection: close\r\n Content-Type: application/x-www-form-urlencoded\r\n User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: */*",
        'content' => $post_string));
    $stream_context = stream_context_create($context);
    $data = file_get_contents($url, false, $stream_context);
    return $data;
}

    $i = 0;
    $conn = mysqli_connect('121.41.23.82' , 'root' , 'weicedbmojito160824','weixin_didao_wxapp');
    $query = "Set Names 'utf8mb4'";
    mysqli_query($conn,$query);
    {
        $keyword1="622出逃计划";
        $keyword2="";
        $keyword3="";
        $keyword4="";
        $keyword5="";
    }
    $query = "SELECT openid,form_id FROM didao_wxapp_party_attender WHERE openid = 'oAtoX0dbKA5QUBn3z0X-5ritpPik'";

    $result = mysqli_query($conn,$query);

    while ($row = mysqli_fetch_assoc($result)){
        $openid = $row['openid'];
        $formId = $row['form_id'];
        $results = mysqli_query($conn,"select * from weixin_token where appID='wxf2961c5a99aa520a' and expireTime > now()");
        $rows = mysqli_fetch_assoc($results);
        $access_token = $rows['token'];

        $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=$access_token";
//        echo $access_token;
        $template= array(
            'touser' => "$openid",
            'template_id' => '4rJoVjZqi-YNzGRjuwD_Qf7Cdz58kMgvLA14v9XxbFY',
            'form_id' => "$formId",
            "data" => array(
                "keyword1" => array(
                    "value" => "$keyword1",
                    "color" => "#173177"
                ),
                "keyword2" => array(
                    "value" => "$keyword2",
                    "color" => "#173177"
                ),
                "keyword3" => array(
                    "value" => "$keyword3",
                    "color" => "#173177"
                ),
                "keyword4" => array(
                    "value" => "$keyword4",
                    "color" => "#173177"
                ),
                "keyword5" => array(
                    "value" => "$keyword5",
                    "color" => "#173177"
                ),
            )
        );
        echo $url;
        $request = urldecode(json_encode($template));
        $resul = json_decode(dataPost($request, $url),true);
        print_r($resul);
//        echo $row['weixinNick'];
        echo ++$i;
    }


/**
 * Created by PhpStorm.
 * User: a7849
 * Date: 2017/7/19
 * Time: 14:04
 */